#include "WEPrecompile.h"
#include "WEMainViewWindow.h"
#include "WEManipulator.h"
#include "imtSceneObject.h"
namespace iMate
{
	// Required for WX
	IMPLEMENT_CLASS(WEMainViewWindow, WEViewWindow)

	// Required for WX
	BEGIN_EVENT_TABLE(WEMainViewWindow, WEViewWindow)
		EVT_KEY_DOWN(WEMainViewWindow::OnKeyDown)
		EVT_MOUSE_EVENTS(WEMainViewWindow::OnMouseEvent)
	END_EVENT_TABLE()

	
	WEMainViewWindow::WEMainViewWindow(wxWindow* parent,const wxString name,const wxSize& size)
	: WEViewWindow(parent,size,name)
	{
		
	}	
	WEMainViewWindow::~WEMainViewWindow()
	{
	}
	void WEMainViewWindow::addRenderWindow(Ogre::NameValuePairList& params,Ogre::Root *root)
	{
		String handle = Ogre::StringConverter::toString((size_t)((HWND)GetHandle()));
		params["externalWindowHandle"] = handle;
		int width;
		int height;
		GetSize(&width, &height);
		mRenderWindow = root->createRenderWindow("imtMainRenderWindow", width, height, false, &params);
		mRenderWindow->setDeactivateOnFocusChange(false);

	}

	void WEMainViewWindow::createCamera(Ogre::SceneManager *sceneMgr)
	{
		mCamera = sceneMgr->createCamera("WEMainCamera");
		Ogre::Viewport* vp = mRenderWindow->addViewport(mCamera);
		vp->setBackgroundColour(Ogre::ColourValue(60.0f/255.0f, 70.0f/255.0f, 100.0f/255.0f, 1.0f));
		vp->setVisibilityMask(RealSceneObject::VMT_SCENE);
		mCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
		mCamera->setNearClipDistance(5);
		mCamera->setFarClipDistance(3000.0f);	
		mCamera->setFixedYawAxis(true);
		mCamNode = sceneMgr->getRootSceneNode()->createChildSceneNode("WEMainCamera");
		mCamNode->attachObject(mCamera);
		mCamNode->setPosition(100,200,200);
		mCamera->lookAt(Vector3::ZERO);

		mIsInitialized = true;
	}
	void WEMainViewWindow::OnKeyDown(wxKeyEvent& event)
	{	
		WEViewWindow::OnKeyDown(event);
		if (mCurrentManipulator)
			mCurrentManipulator->onManipulateKey(event);
	}

	void WEMainViewWindow::OnMouseEvent(wxMouseEvent& event)
	{
		WEViewWindow::OnMouseEvent(event);
		if (mCurrentManipulator)
			mCurrentManipulator->onManipulateMouse(event);
		
	}


}